package com.kkb.projectauth.controller;

import com.kkb.projectauth.dto.*;
import com.kkb.projectauth.pojo.User;
import com.kkb.projectauth.pojo.UserLoginRegister;
import com.kkb.projectauth.service.UserInfoService;
import com.kkb.projectauth.service.UserLoginRegisterService;
import com.kkb.projectauth.service.UserUpdateService;
import com.kkb.projectauth.service.impl.UserUpdateServiceImpl;
import com.kkb.projectcommon.api.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ZhuZhiwei
 * @Description:
 * @Date: Create in 19:28 2021/4/10
 */

@RestController
@RequestMapping("/auth")
public class UserController {

    @Autowired
    private UserLoginRegisterService userLoginRegisterService;

    @Autowired
    private UserUpdateService userUpdateService;

    @Autowired
    private UserInfoService userInfoService;

//    @Autowired
//    private UserService userService;

//    @Autowired
//    private RestTemplate restTemplate ;
//    @GetMapping("/test")
//    public String test(){
//        String username = "admin";
//        String password = "admin";
//        User user = userService.login(username, password);
//        System.out.println(user);
//        return user.toString();
//    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping(value = "/login")
    public CommonResult login(@Validated @RequestBody UserLoginParam userLoginParam){
        String token = userLoginRegisterService.login(userLoginParam.getUsername(),userLoginParam.getPassword());
        if(token == null){
            return CommonResult.validateFailed("用户名或者密码错误");
        }
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        //TODO put tokenHead
        return CommonResult.success(tokenMap);
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @GetMapping("/sendsms")
    public String sendSms() {
//        return restTemplate.getForObject("localhost:9050//test",String.class);
        return "";
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping(value = "/loginByCode")
    public CommonResult loginByCode(@Validated @RequestBody UserLoginByCodeParam userLoginByCodeParam) {
        String token = null;
        //TODO service.loginByCode(phone,smsCode)
        //错误：1.手机未发送验证码，2，验证码错误
        if(token == null){
            return CommonResult.validateFailed("手机号或者验证码错误");
        }
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        //TODO put tokenHead
        return CommonResult.success(tokenMap);
    }

    /**
     * 注册
     * @param userRegisterParam 用户注册传参
     * @return CommentResult
     */
    @PostMapping("/user/register")
    public CommonResult register(@Validated @RequestBody UserRegisterParam userRegisterParam) {
        String phone = userRegisterParam.getPhone();
        String password = userRegisterParam.getPassword();
        UserLoginRegister userLoginRegister = userLoginRegisterService.register(phone, password);
        if (userLoginRegister != null) {
            // 返回失败结果
            return CommonResult.failed("用户已存在，注册失败！");
        }
        int insert = userLoginRegisterService.insertUser(phone, password);
        if (insert > 0) {
            return CommonResult.success("注册成功");
        }
        return CommonResult.failed("注册失败！");
    }

    /**
     * 通过id查询 用户姓名、手机号、微信号
     * 、用户姓名、微信号
     * @param id 用户唯一标识符
     * @return CommentResult
     */
    @GetMapping("user/{id}")
    public CommonResult getUserInfo(@PathVariable Long id) {
        HashMap<String,String> hashMap = userInfoService.getUserInfo(id);
        if (hashMap == null) {
            return CommonResult.failed("获取信息失败，用户不存在");
        }
        return CommonResult.success(hashMap,"获取信息成功");
    }


    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @GetMapping("/user/icon/{id}")
    public CommonResult getUserIcon(@Validated @PathVariable Long id) {
        HashMap<String,String> hashMap = userInfoService.getUserICon(id);
        if (hashMap != null) {
            return CommonResult.success(hashMap,"获取成功");
        }
        return CommonResult.failed("获取用户头像失败");
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping("/user/update/icon/{id}")
    public CommonResult updateUserIcon(@Validated @PathVariable Long id, @Validated @RequestBody UserUpdateIconParam userUpdateIconParam) {
        String iconUrl = userUpdateIconParam.getImage_hard();
        Boolean b = userUpdateService.updateIconUrl(id, iconUrl);
        if (b == true) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping("/user/update/userphone/{id}")
    public CommonResult updateUserPhone(@Validated @PathVariable Long id, @Validated @RequestBody UserUpdatePhoneParam userUpdatePhoneParam) {
        String phone = userUpdatePhoneParam.getPhone();
        return userUpdateService.updatePhone(id, phone);
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping("/user/update/username/{id}")
    public CommonResult updateUsername(@Validated @PathVariable Long id, @Validated @RequestBody UserUpdateUsernameParam userUpdateUsernameParam) {
        String username = userUpdateUsernameParam.getUsername();
        UserLoginRegister userLoginRegister = userUpdateService.updateUsername(id, username);
        if (userLoginRegister == null) {
            int i = userUpdateService.updateById(id, username);
            if (i > 0) {
                return CommonResult.success("更换成功用户名");
            } else {
                return CommonResult.failed("更换用户名失败");
            }
        } else {
            return CommonResult.failed("用户名已存在");
        }
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping("/user/update/vxnumber/{id}")
    public CommonResult updateUserVxNumber(@Validated @PathVariable Long id, @Validated @RequestBody UserUpdateVxNumberParam userUpdateVxnumberParam) {
        String wx_card = userUpdateVxnumberParam.getWx_card();
        int i = userUpdateService.updateUserVxNumber(id, wx_card);
        if (i > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    /**
     * 根据用户ID获取用户头像
     * @param id 用户唯一标识符
     * @return CommenResult
     */
    @PostMapping("/user/update/usertype/{id}")
    public CommonResult updateUserType(@PathVariable Long id, @Validated @RequestBody UserInsertTypeParam userInsertTypeParam) {
        Byte type = userInsertTypeParam.getType();
        int i = userUpdateService.insertType(id, type);
        if (i > 0) {
            return CommonResult.success("选择身份成功");
        }
        return CommonResult.failed("选择身份失败");
    }

}
